/*
 * ============================================================================
 *
 *       Filename:  compress.cpp
 *
 *    Description:  compress ip location data to QQWry.Dat file
 *
 *         Author:  liangqing
 *
 * ============================================================================
 */
#include "IPRecord.h"
#include "IPRecordReader.h"
#include "QQWryCompress.h"
#include "QQWryLookup.h"
#include <iostream>

#ifdef WIN32
#include "IPRecordThreadReader.h"
#include <windows.h>
#define GET_TIME(p) (p)=GetTickCount()
#else
#include <sys/time.h>
#define GET_TIME(p) do{\
                        struct timeval __tv;\
                        gettimeofday(&__tv, NULL);\
                        (p)=(__tv.tv_sec*1000)+(__tv.tv_usec/1000);\
                    }while(0)
#endif /* WIN32 */

using std::cout;
using std::endl;

void test_reader() {
    IPRecord _record;
    IPRecord *record = &_record;
    IPRecordReaderQueue reader("-CZ88.NET");
    reader.push_reader(IPRecordReaderQueue::textfile, "test.txt");
    while(reader.get(record)) {
        if(!record->validate()) continue;
        cout << *record << '\n';
    }
}

void compress() {
    long start_time, stop_time;
    GET_TIME(start_time);
    
    IPRecord _record;
    IPRecord *record = &_record;
    IPRecordReaderQueue reader(NULL);

    //multi-thread for reading and writing in windows
    //it's more slowly than single-thread...
    //IPRecordThreadReader reader(NULL, 1024);
    
    reader.push_reader(IPRecordReaderQueue::textfile, "origin.txt");
    //reader.push_reader(IPRecordReaderQueue::textfile, "test.txt");
 
    QQWryCompress cmprs("QQWry.Dat");

    while(reader.get(record)) {
        if(!record->validate()) continue;
        cmprs.put(record);
    }
    cmprs.finish();

    GET_TIME(stop_time);

    cout << "total number of records: " << cmprs.total() << endl;
    cout << "file size: " << cmprs.file_size() << endl;
    cout << "running time: " << (stop_time - start_time)/1000.0 <<"s." <<endl;
}

void test_qqwry_lookup() {
    long start_time, stop_time;
    GET_TIME(start_time);

    IPRecord _record;
    IPRecord *record = &_record;
    QQWryLookup reader("QQWry.Dat");
    int count = 0;
    //while(reader.get(record)) {
        //count++;
        //cout << *record << '\n';
        //if(count > 100) break;
    //}
    count = reader.read_all();

    GET_TIME(stop_time);

    cout << "total number of records: " << count << endl;
    cout << "running time: " << (stop_time - start_time)/1000.0 <<"s." <<endl;
}

int main() {
    //test_reader();
    //test_qqwry_lookup();
    compress();
}
